package controllers;

import view.DisplayList;
import view.V_Applet;

/*********************************************************
*    CONTROL MANAGER
*  This class manages all the controllers, both UI and feature. It calls their onInteract method when there is a mouse interaction preformed by the user.
*  the interID determines what action to preform. "Released" is called during draw() while "leftClick", "rightClick", and "doubleClick" are triggered as events.
*   
*  updateUIControllers. methods each time an update should be preformed.
**********************************************************/
public class ControlManager
{
  private static final boolean DYNAMIC_REDRAW = false; // this feature is still in development. Enable to test	
	
  private C_Control[] controlList = new C_Control[0]; //A list of all controllers managed by this controlManager. 
  private DisplayList displayList; // the display list that this control manager is associated with.
  private boolean doRemoval = false; // whether to remove controllers this iteration
  private C_Control[] c_remove;  // a removal cue
  private V_Applet container; // used to handle draw management
  
  // Default constructor
  public ControlManager(V_Applet contents, DisplayList dList)
  {
    c_remove = new C_Control[0];
    displayList = dList;
    container = contents;
  }
  // Create with a specified display list for the control manager
  public ControlManager(DisplayList dList)
  {
    displayList = dList;
    c_remove = new C_Control[0];
  }
  
  public void addController (C_Control controller)
  {
	C_Control[] tmpList = controlList; // create a copy of the list
	controlList = new C_Control[controlList.length+1]; // make room for new element
	for(int i = 0; i < tmpList.length; i++) {
		controlList[i] = tmpList[i]; // add the elements back in
	}
	controlList[tmpList.length] = controller; // add the new element to the end  
    //controlList = (C_Control[])append(controlList, controller);
  }
  
  public void removeController(C_Control cont)
  {
      C_Control[] tmpList = controlList;
      controlList = new C_Control[tmpList.length-1];
      int s = 0;
      for(int e = 0; e < tmpList.length; e++)
      {
        if(tmpList[e].equals(cont) == false)
          controlList[e-s] = tmpList[e];
        else s = 1; // compensate for array going out-of-bounds
      }
      return;
  }
  // Necessary to cue the removal of controllers rather than removing them immediately:  
  public void cueRemoval (C_Control controller)
  {
    C_Control[] tmpList = c_remove; // create a copy of the list
    c_remove = new C_Control[c_remove.length+1]; // make room for new element
	for(int i = 0; i < tmpList.length; i++) {
		c_remove[i] = tmpList[i]; // add the elements back in
	}
	c_remove[tmpList.length] = controller; // add the new element to the end    
    //c_remove = (C_Control[])append(c_remove, controller);
    doRemoval = true;
  }
 
  // -------------------------------
  //        GETs & SETs:
  // -------------------------------
  public DisplayList getDisplayList()
  {
    return displayList;
  }
  
  // ----------------------------------------------------------------------
  //     MAIN UPDATE METHOD
  //     takes the x and y positions of the mouse and the current state of the mouse and passes those parameters to each controller object 
  //     Has been adapted to provide for priority-based selection
  // ----------------------------------------------------------------------
  public void updateControllers(float mouseXLoc, float mouseYLoc, String interID)
  {
     for (int i=0; i < controlList.length; i++)
     {
       controlList[i].onInteract(mouseXLoc, mouseYLoc, interID);
     }
  }
  
  public void rightClickEvent() { // Trigger a rightClickEvent for all controllers
    for (int i=0; i < controlList.length; i++)
     {
       controlList[i].rightClickEvent();
     }
  }
  
  public void reDraw()
  {
	  if(DYNAMIC_REDRAW)
	  {
		  container.reDraw();
	  }
  }
}
